//package com.plian.system.service.sys.warning.impl;
//
//import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
//import com.plian.Tools.DateUtil;
//import com.plian.Tools.office.WordUtil;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.entity.sys.warning.WarningReport;
//import com.plian.system.mapper.sys.warning.WarningReportMapper;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.file.FileService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.sys.warning.IWarningReportService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.sys.warning.WarningReportVO;
//import com.plian.system.wrapper.sys.warning.WarningReportWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.env.Environment;
//import org.springframework.mock.web.MockMultipartFile;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.util.*;
//import java.util.concurrent.CompletableFuture;
//
//@Service
//@AllArgsConstructor
//@Slf4j
//public class WarningReportServiceImpl extends BaseServiceImpl<WarningReportMapper, WarningReport> implements IWarningReportService {
//
//    private final FileService fileService;
//
//    private final FormWorkflowService formWorkflowService;
//
//    private final IFormCodeService formCodeService;
//
//    private final IOrgService orgService;
//
//    private Environment environment;
//
//    private final WarningReportWrapper warningReportWrapper;
//
//
//    @Override
//    public boolean submit(WarningReportVO reportVO) {
//        WarningReport report = warningReportWrapper.voToEntity(reportVO);
//        if (!Optional.ofNullable(report.getStatus()).isPresent()) {
//            Org org = orgService.getById(report.getWarningOrg());
//            report.setStatus(FormStatusConstant.UNREVIEWED);
//            report.setReportStatus(0);
//            report.setReportApplyDate(new Date());
//            report.setFormType(String.valueOf(FormTypeConstant.SYS_WARN_MESSAGE_REPORT));
//            report.setReportName(DateUtil.formatDate(report.getWarningStartDate()) + "-" + DateUtil.formatDate(report.getWarningEndDate()) + " " + org.getName());
//            report.setReportType(org.getName());
//        }
//        return saveOrUpdate(report);
//    }
//
//    @Override
//    public boolean saveOrUpdate(WarningReport warningReport) {
//        boolean flag = super.saveOrUpdate(warningReport);
//        CompletableFuture.runAsync(() -> {
//            createSyncWarningReport(warningReport);
//        });
//        return flag;
//    }
//
//    /**
//     * 生成报告
//     *
//     * @return
//     * @param:
//     * @date: 2023/4/27 5:53 PM
//     * @author: jatpeo
//     */
//    private void createSyncWarningReport(WarningReport warningReport) {
//        //data
//        Map<String, String> data = new HashMap<>();
//        data.put("businessModule", warningReport.getBusinessModule().toString());
//        data.put("warningOrg", warningReport.getWarningOrg());
//        data.put("warningDate", DateUtil.formatDate(warningReport.getWarningStartDate()) + " " + DateUtil.formatDate(warningReport.getWarningEndDate()));
//        data.put("reportName", warningReport.getReportName());
//        data.put("reportType", warningReport.getReportType());
//        //读取模版
//        File file = null;
//        try {
//            //生成附件信息
//            String realPath = environment.getProperty("doc.path") + warningReport.getId();
//            //模版
//            InputStream demo = this.getClass().getResourceAsStream("/pdf/warning_message_report.docx");
//
//            file = WordUtil.createReportFile(data, realPath, warningReport.getReportName() + "预警报告.docx", demo);
//            InputStream input = new FileInputStream(file);
//            MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), "application/octet-stream", input);
//            List<HashMap> listDoc = new ArrayList<>();
//            HashMap<String, Object> doc = new HashMap<>();
//            doc.put("itFileName", warningReport.getReportName() + "预警报告.docx");
//            doc.put("filePath", realPath);
//            doc.put("fileSize", multipartFile.getSize());
//            doc.put("mime", "application/msword");
//            doc.put("ext", ".doc");
//            listDoc.add(doc);
//            //保存附件内容
//            fileService.saveAll(String.valueOf(warningReport.getId()), listDoc);
//            log.info("success:保存附件成功...");
//            //修改附件生成状态
//            update(new LambdaUpdateWrapper<WarningReport>().eq(WarningReport::getId, warningReport.getId())
//                    .set(WarningReport::getReportStatus, 1));
//        } catch (Exception e) {
//            log.error("error:生成附件失败！{}", e);
//            throw new RuntimeException(e);
//        }
//
//
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            WarningReport warningReport = getById(id);
//            if (warningReport == null) {
//                return false;
//            } else if (warningReport.getStatus() != null &&
//                    (warningReport.getStatus() != FormStatusConstant.STASH && warningReport.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//}
